package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.Account;

@Repository
public class AccountDaoImpl {
	
	@Autowired 
	private SessionFactory sessionFactory;
	
	public void addAccount(Account account) {
		try{
		sessionFactory.getCurrentSession().save(account);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public Account getAccount(Integer id) {
		return (Account)sessionFactory.getCurrentSession().get(Account.class, id);
	}
	public Account getAccountByName(String accountName) {
			Session session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("from Account where accountName=  :accountName");
			query.setParameter("accountName", accountName);
			List<Account>list = new ArrayList<Account>();
			list = query.list();
			if(list.size()>0){
				return list.get(0);
				
			}else{
				return null;
			}
	}
	public void saveOrUpdate(Account account) {
		sessionFactory.getCurrentSession().saveOrUpdate(account);
	}
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountByEmpId(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account where accountCreator.id = :id and isDeleted = false") ;
		query.setParameter("id", id);
		List<Account> accounts = new ArrayList<Account>();
		accounts = query.list();
		return accounts;
		
	}
	
	@SuppressWarnings("unchecked")
	public Account getAccountByEmpId(Integer id) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account where accountCreator.id = :id and isDeleted = false") ;
		query.setParameter("id", id);
		List<Account> accounts = new ArrayList<Account>();
		accounts = query.list();
		if(accounts.size()>0){
			return accounts.get(0);
		}
		else{
			return null;
		}
		
	}
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccount(Integer index) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account where isDeleted= :isDeleted");
		query.setParameter("isDeleted", false);
		query.setFirstResult((index*5));
		query.setMaxResults(5);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts;
		
		
		
		
	}
	
	// get accounts for this company only 
	
	@SuppressWarnings("unchecked")
	public List<Account> getAllAccountForCompany(Integer index,String companyName,Integer count) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account acc where isDeleted= :isDeleted and acc.accountCreator.company.companyName = :companyName ");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		query.setFirstResult((index*count));
		query.setMaxResults(count);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts;
		
		
		
		
	}
	@SuppressWarnings("unchecked")
	public Integer getAllAccountSize() {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account  where isDeleted= :isDeleted");
		query.setParameter("isDeleted", false);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts.size();

	}
	
	@SuppressWarnings("unchecked")
	public Integer getAllAccountForCompanySize(String companyName) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Account acc where isDeleted= :isDeleted and acc.accountCreator.company.companyName = :companyName ");
		query.setParameter("isDeleted", false);
		query.setParameter("companyName", companyName);
		List<Account> accounts= new ArrayList<Account>();
		accounts = query.list();
		return  accounts.size();

	}

	public void deleteAccount(Integer id) {
		Account account = (Account) sessionFactory.getCurrentSession().load(
				Account.class, id);
		if (null != account) {
			sessionFactory.getCurrentSession().delete(account);
		}
		
	}


}
